package city.spring.modules.material.controller;

import city.spring.modules.material.entity.ShelfEntity;
import city.spring.modules.material.entity.ShelflevelEntity;
import city.spring.modules.material.service.ShelfService;
import city.spring.modules.material.service.ShelflevelService;
import city.spring.modules.material.service.StorehouseService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：货架层次表
 *
 * @author 谢金婷
 * @date 2020-06-14 16:27:53
 */
@RestController
@RequestMapping("/shelflevel")
public class ShelflevelController extends ApiController {
    private final ShelflevelService shelflevelService;
    private final StorehouseService storehouseService;
    private final ShelfService shelfService;

    /**
     * 实体类可排序字段
     */
    private final List<SFunction<ShelflevelEntity, ?>> entityOrderFields;

    public ShelflevelController(ShelflevelService shelflevelService, StorehouseService storehouseService, ShelfService shelfService) {
        this.shelflevelService = shelflevelService;
        this.storehouseService = storehouseService;
        this.shelfService = shelfService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(ShelflevelEntity::getId);
        entityOrderFields.add(ShelflevelEntity::getCode);
        entityOrderFields.add(ShelflevelEntity::getGmtCreate);
        entityOrderFields.add(ShelflevelEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>货架表</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, ShelflevelEntity entity) {
        LambdaQueryChainWrapper<ShelflevelEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>货架表</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, ShelflevelEntity entity) {
        LambdaQueryChainWrapper<ShelflevelEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<ShelflevelEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        for (ShelflevelEntity record : page.getRecords()) {
            try {
                ShelfEntity shelfEntity = shelfService.getById(record.getShelfId());
                record.setShelfname(shelfEntity.getSname());
            } catch (Exception ignore) {
                record.setShelfId(null);
                record.setShelfname("< 找不到货架 >");
            }
        }
        return success(page);
    }

    /**
     * 获取一个 <strong>货架表</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(shelflevelService.getById(id));
    }

    /**
     * 添加一个 <strong>货架表</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody ShelflevelEntity entity) {
        shelflevelService.saveShelflevel(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>货架表</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody ShelflevelEntity entity) {
        entity.setId(id);
        shelflevelService.updateShelflevel(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>货架表</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        shelflevelService.deleteShelflevel(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>货架表</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        shelflevelService.deleteShelflevel(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<ShelflevelEntity> buildLambdaQuery(ShelflevelEntity entity) {
        LambdaQueryChainWrapper<ShelflevelEntity> lambdaQuery = shelflevelService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), ShelflevelEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getCode()), ShelflevelEntity::getCode, entity.getCode());
//        lambdaQuery.like(entity.getStorehouseId() != null, ShelfLevelEntity::getStorehouseId, entity.getStorehouseId());
        lambdaQuery.eq(entity.getGmtCreate() != null, ShelflevelEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, ShelflevelEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<ShelflevelEntity> buildQuery(ShelflevelEntity entity) {
        QueryWrapper<ShelflevelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.eq(entity.getShelfId() != null, "shelf_id", entity.getShelfId());
        wrapper.like(StringUtils.isNotBlank(entity.getCode()), "code", entity.getCode());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

    /**
     * 根据物资id查询货架层次信息
     *
     * @param pageable
     * @param materialId
     * @return
     */
    @GetMapping("/getShelflevelByMtid")
    public Object getShelflevelByMtid(Pageable pageable, String materialId) {
        return success(shelflevelService.getShelflevelByMtid(pageable, materialId));
    }

    /**
     * 根据shelfid查找货架层次
     *
     * @param pageable
     * @param shelfId
     * @return
     */
    @GetMapping("/getShelflevelByShelfId")
    public Object getShelflevelByShelfId(Pageable pageable, String shelfId) {
        return success(shelflevelService.getShelflevelByShelfId(pageable, shelfId));
    }
}
